package com.focus.xyz.union.pages.frontend.tables;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseTable;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/23.
 */
public class TradeRecordTable extends BaseTable {
    private WebDriver driver;

    @FindBy(xpath = "//form[@id='trans_form']/table/tbody")
    private WebElement tradeRecordTab;
    public TradeRecordTable(WebDriver driver,Action action) {
        super(action);
        this.driver = driver;
        PageFactory.initElements(driver,this);
    }

    @Override
    protected By getLinkLocator() throws Exception {
        return By.xpath("td[5]/a");
    }

    @Override
    protected By getLocator(int index) throws Exception {
        return By.xpath("td[" + index + "]");
    }


    public boolean checkStatus() throws Exception{
        boolean flag = true;
        List<WebElement> list = tradeRecordTab.findElements(By.tagName(Constants.TAG_TR));
        for(int i = 1;i<list.size();i++){
            if(!Constants.APP_STAT_A.equals(list.get(i).findElement(By.xpath("td[5]")).getText().trim())){
                flag = false;
                break;
            }
        }
        return flag;
    }

    /*交易记录费用校验*/
    public boolean checkFeeRate(String settleType,String expectedPrice,String insuredId,String feeRate) throws Exception{
        double d = Double.parseDouble(expectedPrice);
        double r = Double.parseDouble(feeRate)/100;
        String[] ss = insuredId.split(",");
        boolean flag = true;
        List<WebElement> list = tradeRecordTab.findElements(By.tagName(Constants.TAG_TR));
        for(int i = 1;i<list.size();i++){
            String ff = list.get(i).findElement(By.xpath("td[6]")).getText().substring(1).trim();
            System.out.println(d*r/ss.length + "-----" + ff + ".0");
            if(Constants.S_T_A.equals(settleType)){
                if(!String.valueOf(d*r/ss.length).equals(ff + ".0")){
                    flag = false;
                    break;
                }
            }else{
                ff = list.get(i).findElement(By.xpath("td[6]")).getText().trim();
                if(!"-".equals(ff)){
                    flag = false;
                    break;
                }

            }
        }
        return flag;
    }

    public String getAppFee(String key) throws Exception{
        return getCellText(tradeRecordTab,key,6).substring(1).trim();
    }

    public int getTradeCount() throws Exception{
        int c = 1;
        List<WebElement> list = tradeRecordTab.findElements(By.tagName(Constants.TAG_TR));
        if(!list.isEmpty()){
            c = list.size();
        }
        return c-1;
    }
}
